<?php

namespace app\common\model;

use think\Db;
use think\Model;


class Acctmoneylog extends Model
{





    // 表名
    protected $name = 'acct_money_log';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [];




    /**
     * 变更会员积分
     * @param int    $money   积分
     * @param int    $user_id 会员ID
     * @param string $memo    备注
     */
    public static function money($money, $user_id, $memo, $image = '', $source = '')
    {
        Db::startTrans();
        try {
            $user = Admin::lock(true)->find($user_id);
            if ($user && $money != 0) {
                $before = $user->revenue;
                $after = $user->revenue + $money;
                //更新会员信息
                $user->save(['revenue' => $after]);
                //写入日志
                self::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo, 'image' => $image, 'image' => $image, 'source' => $source]);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
    }
}
